---
title: "3. Write your first query"
---

The most common GraphQL operation is the **query**, which requests data from your graph in a structure that conforms to your server's schema. If you return to [the Sandbox](https://studio.apollographql.com/sandbox/explorer?endpoint=https%3A%2F%2Fapollo-fullstack-tutorial.herokuapp.com%2Fgraphql) for your server, you can see available query fields in the Schema Reference tab you opened earlier.

Scroll down to the `launches` field to get details about it:

<img src="images/launches_detail.png" class="screenshot" alt="Detail about launches query"/>

Here, you see both the field name itself, its return type, and information about parameters that can be passed to it. You can use this information to write a query you'll eventually add to your app.

To start working with this field in the Sandbox Explorer, select the "play" button to the right side of the information:

<img src="images/open_in_explorer_launches.png" class="screenshot" alt="Open in Explorer"/>

This brings you back into Sandbox's Explorer tab with the sidebar on the left showing documentation for the field you've selected:

<img src="images/explorer_sandbox_open.png" class="screenshot" alt="Docs open in the left sidebar"/>

Notice the small plus button next to the `launches` field. Click this button to add the field to the middle "operations" panel:

<img src="images/explorer_add_launches_query.png" class="screenshot" alt="Click the button to add this query"/>

When the field is added, it will look like this:

<img src="images/explorer_initial_added_query.png" class="screenshot" alt="The query once it's been added to the Operations section"/>

Let's break down what you're seeing here:

- The type of the operation, `query`, followed by the name of the operation, currently `Query` (we'll make that more specific in a second), is the outermost set of braces.
- The actual field being selected is the next set of braces in. Since the **arguments** for this field both have default values, they are not automatically added for you.
- An error in the empty space between the braces, which is where you'll put the list of information you want back from each launch.

The Apollo Kotlin SDK requires every query to have a name (even though this isn't required by the GraphQL spec). Since you're going to create more than one query, it's also a good idea to give this operation a specific name other than `Query`. Change the name of the operation to `LaunchList`:

<img src="images/explorer_launch_list_rename.png" class="screenshot" alt="Renaming the query"/>

Next, on the left hand side, you can select what fields you want back in the returned object. Start by clicking the plus button next to the `cursor` field. It will mark that field as selected, then insert it into your operations:

<img src="images/explorer_check_cursor.png" class="screenshot" alt="After adding the cursor field."/>

This is probably the easiest way to add fields to your object, since it knows how everything is spelled and what type everything is.

However, you can also use auto-complete to help you with this. Add a newline below `cursor` in the Operations panel and start typing `ha`. An autocomplete box pops up and shows you options based on what's in the schema:

<img src="images/explorer_autocomplete.png" class="screenshot" alt="Example of autocomplete"/>

The Sandbox Explorer is a great tool for building and verifying queries so you don't have to repeatedly rebuild your project in Android Studio to try out changes.

As the schema indicates, the `launches` field returns a `LaunchConnection` object. This object includes a list of launches, along with fields related to pagination (`cursor` and `hasMore`). The query you've written so far indicates exactly which fields of this `LaunchConnection` object you want to be returned.

Run this query by pressing the "Submit Operation" button, which should now have the name of your query, `LaunchList`:

<img src="images/explorer_submit_operation.png" class="screenshot" alt="Submit the operation"/>

You'll quickly see the query returns results as a JSON object on the right-hand side of the page:

<img src="images/explorer_launch_list_initial_response.png" class="screenshot" alt="Query JSON in Sandbox Explorer"/>

This query executes successfully, but it doesn't include any information about the `launches`! That's because we didn't include the necessary fields in the query.

Click the button next to the `launches` field at the bottom of the left column. It will add a set of braces for `launches` to the operations section, and then move the documentation to show information for the `Launch` type:

<img src="images/explorer_launches_drill_in.png" class="screenshot" alt="Status after adding launches field"/>

The fields you add in this set of braces will be fetched for every launch in the list. Click the buttons next to `id` and `site` properties to add those two fields. When you're done, your operation should look like this:

```graphql title="(Sandbox Explorer)"
query LaunchList {
  launches {
    cursor
    hasMore
    launches {
      id
      site
    }
  }
}
```

Run the operation again, and you'll now see that in addition to the information you got back before, you're also getting a list of launches with their ID and site information:

<img src="images/completed_id_query.png" class="screenshot" alt="Updated query JSON in Sandbox Explorer"/>

## Add the query to your project

Now that your query is fetching the right data, head back to Android Studio.

1. Right click on the `src/main/graphql/` folder. This folder should contain your `schema.graphqls`. Select **New > File**:

<img src="images/new_file.png" class="screenshot" alt="New GraphQL file"/>

2. Name the file `LaunchList.graphql`. Make sure it's saved at the same level as your `schema.graphqls` file.

3. Copy your final query from Sandbox Explorer and paste it into `LaunchList.graphql`.

```graphql title="app/src/main/graphql/LaunchList.graphql"
query LaunchList {
  launches {
    launches {
      id
      site
    }
  }
}
```

## Generate the model

Build your project to have the Apollo Kotlin plugin generate your first model. The plugin defines a task named `generateApolloSources` to generate the models. You don't need to run it. It will be executed automatically when building your project.

> **Note**: Autocomplete won't work until you build your project. That is because autocomplete requires the generated code to work. Each time you change your queries, you should rebuild your project for Android Studio to pick up the modifications.

## Examine generated code

From the menu, select **Navigate > Class** and start typing `LaunchList`, Android Studio should suggest to open `LaunchListQuery.kt`. The file should be in `app/build/generated/source/apollo/service/com/example/rocketreserver/LaunchListQuery.kt`.

The `LaunchListQuery.kt` file defines a root class, `LaunchListQuery`, with many nested classes. If you compare the classes to the JSON data returned in Sandbox Explorer, you see that the structure matches. These classes include properties only for the fields that your query requests.

Try commenting out the `id` property in `LaunchList.graphql`, saving, then building again. When the build completes, the `Launch` class now only includes the requested `site` property.

Uncomment `id` and rebuild to restore the property.

Now that you've generated code and had a chance to see what's in there, it's time to [execute the query](04-execute-the-query)!
